package com.kgc.scd.producer;

import com.kgc.scd.bean.ArrivalUser;
import com.kgc.scd.bean.NotifyProArrive;
import com.kgc.scd.contant.ArrivalRemindConstant;
import com.kgc.scd.service.NotifyProArriveService;
import lombok.extern.slf4j.Slf4j;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;

import javax.jms.Destination;
import java.util.List;

/**
 * Created On : 2022/6/16.
 * <p>
 * Author     : nainai
 * <p>
 * Description: 到货提醒，发布消息者
 */
@Slf4j
@Component
public class ArrivalRemindProducer {

    @Autowired(required = false)
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private NotifyProArriveService notifyProArriveService;

    /**
     * @author : nainai
     * @date   : 2022/6/16
     * @param  : [java.lang.String, java.lang.String]
     * @return : void
     * @description : 到货提醒
     */
    public void sendArrivalRemindMsg(String proId) {
        log.info("****** 商品{}到货，发送消息到队列：{} ******", proId, ArrivalRemindConstant.ARRIVAL_REMIND_QUEUE_NAME);

        // 创建一个目标消息队列对象（消息缓存容器，缓存发送的消息体）
        Destination destination = new ActiveMQQueue(ArrivalRemindConstant.ARRIVAL_REMIND_QUEUE_NAME);

        // 发送消息到目标队列
        jmsMessagingTemplate.convertAndSend(destination, proId);
    }

    @JmsListener(destination = ArrivalRemindConstant.ARRIVAL_REMIND_RESP_QUEUE_NAME)
    public void returnSendArrivalRemindMsg(List<ArrivalUser> arrivalUsers){
        log.info("发送到货提醒成功，修改表格");
        // 修改为已发送状态
        for (ArrivalUser a : arrivalUsers) {
            NotifyProArrive notifyProArrive = NotifyProArrive.builder()
                    .id(a.getId())
                    .status(1)
                    .build();
            notifyProArriveService.modNotifyProArriveById(notifyProArrive);
        }
    }

}
